home *** CD-ROM | disk | FTP | other *** search
- Path: pegasus.montclair.edu!harmon
- From: harmon@pegasus.montclair.edu (Derek Harmon)
- Newsgroups: comp.lang.c
- Subject: Re: 'Freeing' storage with realloc
- Date: 16 Feb 1996 00:01:20 -0500
- Organization: Montclair State University
- Message-ID: <harmon.824446299@pegasus.montclair.edu>
- References: <4fv44f$p2v@news.rz.uni-passau.de>
- NNTP-Posting-Host: pegasus.montclair.edu
- Keywords: realloc, memory-managment
- X-Newsreader: NN version 6.5.0 #68 (NOV)
-
- berndl@sidonius.uni-passau.de (Klaus Berndl) writes:
- > char* buf = NULL;
-
- > buf = (char*)malloc(100);
- > strcpy(buf, "Klaus Berndl");
- > printf("\n%s\n", buf);
-
- >/* line 10 */ buf = (char*)realloc(buf, strlen(buf)+1); /* line 10 */
-
- >My questions are now: How much memory is allocated for 'buf' after
- >executing line 10?! Does realloc 'freeing' the storage behind byte 13?
- >Or are still 100 bytes allocated for buf?
-
- Your first premise is correct, realloc() will resize the block of memory
- allocated to buf to be 13 characters (strlen(buf) + 1). The remaining 87
- bytes will be freed. In all likelihood (since buf was involved in the only
- memory allocations in your code), those 87 bytes will be reunited with the
- heap (no fragmentation will result) and buf and the contents of the memory
- buf points to will remain at its present location (no copying to a new location
- will be neccessary, as would be the case if your requested > 100 bytes and
- other data elements occupied the heap).
- -- Stone
- --
- # Derek Harmon (aka Stonelight) harmon@pegasus.montclair.edu
- # - Computer Science Undergrad, Montclair State University, NJ
- # - My views are my own, nobody else is this creative. 3;)>
- ... Word for Windows, from the people who brought you EDLIN.
-
-